package com.integralPerformance.controller;

import com.integralPerformance.common.Result;
import com.integralPerformance.pojo.entity.PointsCriteria;
import com.integralPerformance.pojo.form.add.AddPointsCriteria;
import com.integralPerformance.pojo.form.update.UpdatePointsCriteria;
import com.integralPerformance.pojo.query.GainLevelQuery;
import com.integralPerformance.pojo.query.GetCategoryQuery;
import com.integralPerformance.pojo.query.GetLevelQuery;
import com.integralPerformance.pojo.query.PointsCriteriaQuery;
import com.integralPerformance.service.impl.PointsCriteriaServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/s2/pointsCriteria")
@Tag(name = "业绩类型（四年一次的审核的）表格控制器", description = "业绩类型（四年一次的审核的）表格控制器")
public class PointsCriteriaController {

    @Autowired
    PointsCriteriaServiceImpl pointsCriteriaService;

    @Operation(summary = "①获取业绩类型", description = "①获取业绩类型")
    @GetMapping("/getType")
    public Result<List<String>> getType() {
        return pointsCriteriaService.getType();
    }

    @Operation(summary = "②获取项目级别", description = "②获取项目级别")
    @PostMapping("/getLevelByType")
    public Result<List<String>> getLevelByType(@RequestBody GetLevelQuery getLevelQuery) {
        return pointsCriteriaService.getLevelByType(getLevelQuery.getPerformanceType());
    }

    @Operation(summary = "③获取项目类别", description = "③获取项目类别")
    @PostMapping("/getCategory")
    public Result<List<String>> getCategory(@RequestBody GetCategoryQuery getCategoryQuery) {
        return pointsCriteriaService.getCategory(getCategoryQuery.getPerformanceType(),getCategoryQuery.getProjectLevel());
    }

    @Operation(summary = "④获取获奖等级", description = "④获取获奖等级")
    @PostMapping("/getGainLevel")
    public Result<List<String>> getGainLevel(@RequestBody GainLevelQuery gainLevelQuery) {
        return pointsCriteriaService.getGainLevel(gainLevelQuery.getPerformanceType(),gainLevelQuery.getProjectLevel(),gainLevelQuery.getProjectCategory());
    }

    @Operation(summary = "获取业绩类型树状结构", description = "以层级关系返回业绩类型树状结构数据，适用前端级联选择、表格分组")
    @GetMapping("/getPointsCriteriaTree")
    public Result<List<Map<String, Object>>> getPointsCriteriaTree() {
        return pointsCriteriaService.getPointsCriteriaTree();
    }

    @Operation(summary = "新增接口：根据id获取", description = "根据id获取")
    @GetMapping("/getPointsCriteriaById/{id}")
    public Result<PointsCriteria> getPointsCriteriaById(@PathVariable String id) {
        PointsCriteria byId = pointsCriteriaService.getById(id);
        return Result.success(byId);
    }

    @Operation(summary = "新增接口：批量根据id获取", description = "批量根据id获取")
    @PostMapping("/getPointsCriteriaByList")
    public Result<List<PointsCriteria>> getPointsCriteriaByList(@RequestParam List<String> ids) {

        log.debug("============================");

        List<PointsCriteria> uniquePointsCriteriaList = pointsCriteriaService.listByIds(ids);
        Map<Integer, PointsCriteria> idToPointsCriteriaMap = uniquePointsCriteriaList.stream()
                .collect(Collectors.toMap(PointsCriteria::getId, Function.identity()));

        // 使用原始的String类型ID列表构造最终的结果列表
        List<PointsCriteria> result = ids.stream()
                .map(id -> idToPointsCriteriaMap.get(Integer.valueOf(id)))
                .collect(Collectors.toList());

        return Result.success(result);
    }

    @Operation(summary = "查询获取", description = "查询获取")
    @PostMapping("/getPointsCriteria")
    public Result<?> getPointsCriteria(@RequestBody PointsCriteriaQuery pointsCriteriaQuery) {
        return pointsCriteriaService.getPointsCriteria(pointsCriteriaQuery);
    }

    @Operation(summary = "修改具体类型内容", description = "修改具体类型内容")
    @PostMapping("/updatePointsCriteria")
    public Result<String> updatePointsCriteria(@RequestBody UpdatePointsCriteria updatePointsCriteria) {
        return pointsCriteriaService.updatePointsCriteria(updatePointsCriteria);
    }

    @Operation(summary = "添加一个业绩标准", description = "添加一个业绩标准")
    @PostMapping("/addPointsCriteria")
    public Result<String> addPointsCriteria(@RequestBody AddPointsCriteria addPointsCriteria) {
        return pointsCriteriaService.addPointsCriteria(addPointsCriteria);
    }

    @Operation(summary = "删除一个业绩标准", description = "删除一个业绩标准")
    @DeleteMapping("/deletePointsCriteriaById/{id}")
    public Result<String> deletePointsCriteriaById(@PathVariable String id) {
        return pointsCriteriaService.deletePointsCriteriaById(id);
    }
}
